package cn.evun.tms.generator.task;

import cn.evun.tms.generator.framework.AbstractApplicationTask;
import cn.evun.tms.generator.framework.context.ApplicationContext;
import cn.evun.tms.generator.handler.BaseHandler;
import cn.evun.tms.generator.handler.impl.ControllerHandler;
import cn.evun.tms.generator.model.*;

import java.util.ArrayList;
import java.util.List;

public class ControllerTask extends AbstractApplicationTask {

	private static String CONTROLLER_FTL = "template/Controller.ftl";

	@SuppressWarnings("unchecked")
	@Override
	protected boolean doInternal(ApplicationContext context) throws Exception {
		logger.info("开始生成controller。。。");

		List<ControllerInfo> controllerInfos = (List<ControllerInfo>) context.getAttribute("controllerInfos");

		BaseHandler<ControllerInfo> baseHandler = null;
		for (ControllerInfo controllerInfo : controllerInfos) {
			baseHandler = new ControllerHandler(CONTROLLER_FTL, controllerInfo);
			baseHandler.execute(context);
		}

		logger.info("结束生成controller。。。");
		return false;
	}

	@Override
	protected void doAfter(ApplicationContext context) throws Exception {
		super.doAfter(context);

		List<DtoInfo> dtoInfos = (List<DtoInfo>) context.getAttribute("dtoList");
		List<ServiceInfo> serviceInfos = (List<ServiceInfo>) context.getAttribute("serviceInfos");
		List<EntityInfo> entityInfos = (List<EntityInfo>) context.getAttribute("entityInfos");

		List<ShowPageInfo> showPageInfos = new ArrayList<ShowPageInfo>();
		List<InfoPageInfo> infoPageInfos = new ArrayList<InfoPageInfo>();
		// 组装Dao信息、组装Vo信息
		for (int i = 0; i < entityInfos.size(); i++) {
			EntityInfo entityInfo = entityInfos.get(i);
			ShowPageInfo showPageInfo = new ShowPageInfo();
			showPageInfo.setUrl(entityInfo.getClassName().toLowerCase());
			showPageInfos.add(showPageInfo);

			InfoPageInfo infoPageInfo = new InfoPageInfo();
			infoPageInfo.setUrl(entityInfo.getClassName().toLowerCase());
			infoPageInfos.add(infoPageInfo);
		}
		context.setAttribute("showPageInfos", showPageInfos);
		context.setAttribute("infoPageInfos", infoPageInfos);


	}

}
